package com.luojialong.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luojialong.constant.PermissionStrategy;
import com.luojialong.constant.UserRank;
import com.luojialong.enums.AffairStatus;
import com.luojialong.exception.AffairException;
import com.luojialong.model.domain.ScoreTb;
import com.luojialong.model.request.scoreTb.ScoreQueryRequest;
import com.luojialong.model.request.scoreTb.ScoreSearchMyRequest;
import com.luojialong.model.request.scoreTb.ScoreTbAddRequest;
import com.luojialong.model.vo.scoreTb.ScoreTbInfoVo;
import com.luojialong.resultVo.ResultResponse;
import com.luojialong.service.ScoreTbService;
import com.luojialong.utils.ResultUtils;
import com.luojialong.verification.MyCreateLog;
import com.luojialong.verification.UserPermissionCheck;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/score")
public class ScoreTbController {


    @Resource
    private ScoreTbService scoreTbService;

    @PostMapping("/add")
    @MyCreateLog(doMethod = "评审的打分信息上传")
    @UserPermissionCheck(LeastPrivilege = UserRank.EXPERT_REVIEW, strategy = PermissionStrategy.PROTECT_STRATEGY)
    public ResultResponse<Long> addScoreTb(@RequestBody ScoreTbAddRequest scoreTbAddRequest) {
        long isAdd = scoreTbService.add(scoreTbAddRequest);
        return ResultUtils.success(isAdd);
    }

    @PostMapping("/page")
    @MyCreateLog(doMethod = "分页查看评分的信息情况")
    @UserPermissionCheck(LeastPrivilege = UserRank.EXPERT_REVIEW, strategy = PermissionStrategy.INVADE_STRATEGY)
    public ResultResponse<Page<ScoreTbInfoVo>> getPage(@RequestBody ScoreQueryRequest scoreQueryRequest) {

        QueryWrapper<ScoreTb> queryWrapper = scoreTbService.getQueryWrapper(scoreQueryRequest);
        Page<ScoreTb> page = scoreTbService.page(new Page<>(scoreQueryRequest.getCurrent(), scoreQueryRequest.getPageSize()), queryWrapper);
        return ResultUtils.success(scoreTbService.getVo(page));
    }

    @PostMapping("/get")
    @MyCreateLog(doMethod = "查看自己的评分情况")
    @UserPermissionCheck(LeastPrivilege = UserRank.STUDENT_CONTEST, strategy = PermissionStrategy.PROTECT_STRATEGY)
    public ResultResponse<ScoreTbInfoVo> getMyScoreInfo(@RequestBody ScoreSearchMyRequest scoreSearchMyRequest) {
        Long userId = scoreSearchMyRequest.getUserId();
        ScoreTb byId = scoreTbService.getById(userId);
        return ResultUtils.success(ScoreTbInfoVo.transferVo(byId));
    }

    @GetMapping("/del")
    @MyCreateLog(doMethod = "高级管理员删除评分信息")
    @UserPermissionCheck(LeastPrivilege = UserRank.ADMINISTRATOR_OF_THE_EDUCATION_BUREAU,strategy = PermissionStrategy.PROTECT_STRATEGY)
    public ResultResponse<Boolean> deleteScoreTb(@RequestParam("id") Long id) {
        Boolean b = scoreTbService.del(id);
        if (!b) {
            throw new AffairException(AffairStatus.FAILURE, "评分信息未删除成功");
        }
        return ResultUtils.success(b);
    }



}
